package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.midi.Track;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	//使用云点播
	@Autowired
	private VodService vodService;

	@Autowired
	private TrackInfoService trackInfoService;


	//上传声音
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file){
		//调用服务层方法--根据后边业务的分析,我们分析出这里应该返回的是Map
		Map<String,Object> map=vodService.uploadTrack(file);

		//返回数据
		return Result.ok(map);
	}

	//保存声音
	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		//获取用户id
		Long userId = null == AuthContextHolder.getUserId() ? 1L : AuthContextHolder.getUserId();
		//调用服务层方法
		trackInfoService.saveTrackInfo(trackInfoVo,userId);
		//返回数据
		return Result.ok();
	}

	//声音查询
	@TsLogin
	@Operation(summary = "分页查询声音列表")
	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable("pageNo")Integer pageNo,
									@PathVariable("pageSize")Integer pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery){

		//构建Page对象
		Page<TrackListVo> trackListVoPage=new Page<TrackListVo>(pageNo,pageSize);
		//获取用户id,谁登录查询的
		Long userId = null == AuthContextHolder.getUserId() ? 1L : AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		//调用服务层方法
		IPage<TrackListVo> trackListVoIPage=trackInfoService.findUserTrackPage(trackListVoPage,trackInfoQuery);
		//返回数据
		return Result.ok(trackListVoIPage);

	}

	//删除声音
	//Request URL: http://localhost/api/album/trackInfo/removeTrackInfo/{trackId}
	@Operation(summary = "根据声音Id删除声音数据")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable("trackId")Long trackId){
		//调用服务层方法
		trackInfoService.removeTrackInfo(trackId);
		//返回数据
		return Result.ok();
	}

	//根据声音Id获取声音对象
	//Request URL: http://localhost/api/album/trackInfo/getTrackInfo/{trackId}
	@Operation(summary = "根据声音Id获取声音对象")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable("trackId")Long trackId){
		//调用服务层方法
		TrackInfo trackInfo=trackInfoService.getById(trackId);
		//返回数据
		return Result.ok(trackInfo);
	}

	//修改声音
	//Request URL: http://localhost/api/album/trackInfo/updateTrackInfo/{trackId}
	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId")Long trackId , @RequestBody TrackInfoVo trackInfoVo){
		//调用服务层方法
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		//返回数据
		return Result.ok();
	}

	/**
	 * 根据专辑ID获取专辑声音列表
	 * @param albumId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	//Request URL: http://localhost/api/album/trackInfo/findAlbumTrackPage/751/1/10
	@TsLogin(required = false)
	@Operation(summary = "根据专辑ID获取专辑声音列表")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
	public Result findAlbumTrackPage(@PathVariable("albumId") Long albumId,
									 @PathVariable("pageNo") Integer pageNo,
									 @PathVariable("pageSize")Integer pageSize){
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		//构建分页对象
		Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pageNo, pageSize);
		//调用服务层方法
		IPage<AlbumTrackListVo> iPage=trackInfoService.findAlbumTrackPage(albumTrackListVoPage,albumId,userId);
		//返回数据
		return Result.ok(iPage);
	}


}

